package com.sdry.web.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.entity.permission.UpmsStaffRole;
import com.sdry.service.permission.UpmsStaffRoleService;
/**
 * 
 * @ClassName:StaffController
 * @Description:员工
 * @Author tdd
 * @Date 2018年4月25日下午5:05:43
 * @version 1.0
 */
@Controller
@RequestMapping("/login")
public class LoginController{
	
	 /**注入员工+角色Server*/
	 @Resource UpmsStaffRoleService  upmsStaffRoleService;
	/**
	 * 根据工号和密码查信息
	 * @param scode 工号
	 * @param pass 密码
	 * @throws IOException 
	 */
	@RequestMapping("/login")
	public void login(String scode,String pass,HttpSession session,HttpServletResponse response) throws IOException{
		if (null != scode && null != pass && !scode.equals("") && !pass.equals("") ) {
			UpmsStaffRole staffRole =upmsStaffRoleService.queryByScodePass(scode,pass);
			if(null != staffRole){
				session.setAttribute("user", staffRole);
				response.getWriter().print(1);
			}else{
				response.getWriter().print(0);
			}
		}else{
			response.getWriter().print(0);
		}
	}
	
	/**
	 * 去登录页面
	 * @throws IOException 
	 */
	@RequestMapping("/toLogin")
	public String toLogin(HttpSession session,HttpServletResponse response) throws IOException {
		 //从session中取出用户身份信息
		UpmsStaffRole staffRoleSession = (UpmsStaffRole) session.getAttribute("user");
		if(staffRoleSession != null){
			session.invalidate(); 
		}
		return "/login";
	}
}
